﻿using System;
using SSTU.ViewModel.Core;

namespace SSTU.ViewModel.Item
{
	/// <summary>
	/// View model for university group.
	/// </summary>
	public class GroupItemVM : ViewModelBase
	{
		private string _id;
		private string _shortName;
		private string _longName;
		private FavoriteState _favoriteState;

		/// <summary>
		/// Initializes a new instance of the <see cref="GroupItemVM"/> class.
		/// </summary>
		public GroupItemVM()
			: this(null, null, null, FavoriteState.Unknown)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="GroupItemVM"/> class.
		/// </summary>
		public GroupItemVM(string id, string shortName, string longName, FavoriteState favoriteState)
		{
			Id = id;
			ShortName = shortName;
			LongName = longName;
			FavoriteState = favoriteState;
		}

		/// <summary>
		/// Gets or sets identifier.
		/// </summary>
		public string Id
		{
			get { return _id; }
			set
			{
				if (value != _id)
				{
					RaisePropertyChanging<string>(() => Id);
					_id = value;
					RaisePropertyChanged<string>(() => Id);
				}
			}
		}

		/// <summary>
		/// Gets or sets short name (abbreviation).
		/// </summary>
		public string ShortName
		{
			get { return _shortName; }
			set
			{
				if (value != _shortName)
				{
					RaisePropertyChanging<string>(() => ShortName);
					_shortName = value;
					RaisePropertyChanged<string>(() => ShortName);
				}
			}
		}
		
		/// <summary>
		/// Gets or sets long (full) name.
		/// </summary>
		public string LongName
		{
			get { return _longName; }
			set
			{
				if (value != _longName)
				{
					RaisePropertyChanging<string>(() => LongName);
					_longName = value;
					RaisePropertyChanged<string>(() => LongName);
				}
			}
		}

		/// <summary>
		/// Gets flag which indicates that this group is in list of favorite groups.
		/// </summary>
		public bool IsFavorite
		{
			get
			{
				bool result = FavoriteState == FavoriteState.Favorite;
				return result;
			}
		}

		/// <summary>
		/// Gets or sets value of enumeration, which indicates that this group is in list of favorite groups.
		/// </summary>
		public FavoriteState FavoriteState
		{
			get { return _favoriteState; }
			set
			{
				if (value != _favoriteState)
				{
					RaisePropertyChanging<bool>(() => IsFavorite);
					RaisePropertyChanging<string>(() => FavoriteCommandCaption);
					RaisePropertyChanging<FavoriteState>(() => FavoriteState);
					_favoriteState = value;
					RaisePropertyChanged<bool>(() => IsFavorite);
					RaisePropertyChanged<string>(() => FavoriteCommandCaption);
					RaisePropertyChanged<FavoriteState>(() => FavoriteState);
					if (FavoriteStateChanged != null)
					{
						FavoriteStateChanged(this, null);
					}
				}
			}
		}

		/// <summary>
		/// Gets caption for command which must change (edit) 'FavoriteState'.
		/// </summary>
		public string FavoriteCommandCaption
		{
			get
			{
				string result = null;
				switch (FavoriteState)
				{
					case FavoriteState.NotFavorite:
						result = "добавить в избранное";
						break;
					case FavoriteState.Favorite:
						result = "убрать из избранного";
						break;
					case FavoriteState.ChangingToFavorite:
						result = "добавляется...";
						break;
					case FavoriteState.ChangingToNotFavorite:
						result = "убирается...";
						break;
					default:
						result = "статус не определен";
						break;
				}
				return result;
			}
		}

		/// <summary>
		/// Occurs when favorite state changed.
		/// </summary>
		public event EventHandler FavoriteStateChanged;

		/// <summary>
		/// Clones this instance.
		/// </summary>
		public GroupItemVM Clone()
		{
			var newItem = new GroupItemVM(Id, ShortName, LongName, FavoriteState);
			return newItem;
		}
	}
}
